package com.example.demo.common.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.common.entity.Craft;
import com.example.demo.common.mapper.CraftMapper;
import com.example.demo.common.mapper.CraftMapper;
import com.example.demo.common.service.CraftService;
import com.example.demo.common.service.CraftService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/craft")
public class CraftController {
    @Resource
    CraftMapper craftMapper;
    @Resource
    CraftService craftService;

    //    此注解为增加
    @PostMapping
    public Result<?> save(@RequestBody Craft craft){
        craftService.saveCraft(craft);
        return Result.success();
    }
    //    此注解为更新
    @PutMapping
    public Result<?> update(@RequestBody Craft craft){
        craftService.updateCraft(craft);
        return Result.success();
    }
    //    此注解为删除
    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id){
        craftService.deleteById(id);
        return Result.success();
    }
    //    此注解为查询
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Craft> wrapper = Wrappers.<Craft>lambdaQuery();
        if (StrUtil.isNotBlank(search)){
            wrapper.like(Craft::getRef, search);
        }
        Page<Craft> craftPage = craftMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(craftPage);
    }

}
